import 'package:flutter/material.dart';
import 'package:flutter_magic_planet/common/constant.dart';
import 'package:flutter_magic_planet/component/flip_bottom_bar/flip_bar_item.dart';
import 'package:flutter_magic_planet/component/flip_bottom_bar/flip_box_bar.dart';
import 'package:flutter_magic_planet/module/mine/mine_view.dart';
import 'package:flutter_magic_planet/module/search/search_view.dart';
import 'package:get/get.dart';

import 'main_logic.dart';

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage>
    with AutomaticKeepAliveClientMixin {
  final MainLogic logic = Get.put(MainLogic());
  final List<Widget> tabs = [SearchPage(), MinePage()];

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      key: logic.scaffoldKey,
      body: PageView(
        controller: logic.pageController,
        onPageChanged: (index) {
          logic.setTabSelected(index);
        },
        children: tabs,
      ),
      bottomNavigationBar: Obx(
        () => FlipBoxBar(
          navBarHeight: 60,
          selectedIndex: logic.selectedIndex.value,
          items: [
            FlipBarItem(
              icon: Icon(Icons.home),
              text: Text("首页"),
              frontColor: Colors.white,
              backColor: primaryColor,
            ),
            FlipBarItem(
              icon: Icon(Icons.account_circle),
              text: Text("我的"),
              frontColor: Colors.white,
              backColor: primaryColor,
            ),
          ],
          onIndexChanged: (newIndex) {
            logic.setTabSelected(newIndex);
          },
        ),
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<MainLogic>();
    super.dispose();
  }

  @override
  bool get wantKeepAlive => false;
}
